package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"work/model/mysql"

	"work/workrpc/internal/svc"
	"work/workrpc/workrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *workrpc.RegisterRequest) (*workrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Phone:     in.Phone,
		Password:  in.Password,
		HeadImage: "https://ts1.tc.mm.bing.net/th/id/OIP-C.-rBqkricrrEXBZsFzjxtlQAAAA?cb=ucfimgc2&rs=1&pid=ImgDetMain&o=7&rm=3",
		NickName:  fmt.Sprintf("昵称-%d%d", rand.Intn(100000), time.Now().Unix()),
	}
	err := user.UserRegister(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &workrpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
